﻿using System;
using System.Windows.Forms;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.UC;
using Telerik.WinControls.UI;

namespace PluginMeb
{
    public partial class DlgSchulart : Telerik.WinControls.UI.RadForm
    {
        #region Constants

        public const string ANNOT_SDL = "SdL";
        public const string ANNOT_SSP = "SSP";
        public const string ANNOT_SBA = "SBA";
        public const string ANNOT_SBG = "SBG";
        public const string ANNOT_BBG_NR = "BBG_NR";
        public const string SET_MARK = "1";
        public const string MISS_MARK = "0";
        
        #endregion

        #region Internal Members

        private CodeType schulart;
        private bool IsEditable;
        private UcMlText ucMlText = null;
        private MultiLang schulartName;
        private CodeListType cltSchulArt;

        #endregion

        #region Constructor

        public DlgSchulart(CodeType code, CodeListType clt, bool editable)
        {
            InitializeComponent();
            ucMlText = new UcMlText();
            comboBoxTitle.DropDownControl = ucMlText;
            cltSchulArt = clt;

            IsEditable = editable;
            schulart = code;
            radButtonDelete.Enabled = IsEditable && !IsCodeCreation;
            BindToControls();
        }
        #endregion

        #region Properties

        private bool IsCodeCreation
        {
            // In creation there is no code, only canton terminator
            get { return schulart.Id.Length == 3; }
        }
        #endregion

        #region Bind to controls

        private void BindToControls()
        {
            // Canton
            textBoxCanton.Text = schulart.Id.Substring(schulart.Id.Length - 2, 2);

            // ID
            numTextBoxValue.Text = schulart.Id.Substring(0, schulart.Id.Length - 3);
            numTextBoxValue.ReadOnly = !(IsEditable && IsCodeCreation);

            // Name
            schulartName = schulart.Name.Clone();
            comboBoxTitle.Text = schulart.Name.ToString();
            ucMlText.ML = schulartName;
            comboBoxTitle.IsFinal = !IsEditable;

            // Validity
            BindValidity(numTextBoxValidFrom, GridCreator.ANNOT_S_VF);
            BindValidity(numTextBoxValidTo, GridCreator.ANNOT_S_VT);

            // Combo
            BindCheck(radCheckBoxSdl, ANNOT_SDL);
            BindCheck(radCheckBoxSsp, ANNOT_SSP);
            BindCheck(radCheckBoxSba, ANNOT_SBA);
            BindCheck(radCheckBoxSbg, ANNOT_SBG);

            // BBG
            BindBbgNr(numTextBox1, ANNOT_BBG_NR);
        }

        private void BindCheck(RadCheckBox ckb, string annotType)
        {
            Annotation a = schulart.AnnotationList.GetAnnotationType(annotType);
            ckb.Checked = a != null && a.Title == SET_MARK;
            ckb.Enabled = IsEditable;
        }

        private void BindValidity(NumTextBox tb, string annotType)
        {
            Annotation a = schulart.AnnotationList.GetAnnotationType(annotType);
            tb.Text = a != null ? a.Title : string.Empty;
            tb.ReadOnly = !IsEditable;
        }

        private void BindBbgNr(NumTextBox tb, string annotType)
        {
            Annotation a = schulart.AnnotationList.GetAnnotationType(annotType);
            tb.Text = a != null ? a.Title : string.Empty;
            tb.ReadOnly = !IsEditable;
        }
        #endregion

        #region Buttons Event

        private void radButtonOk_Click(object sender, EventArgs e)
        {
            bool updatable = true;

            // check that required fields are filled 
            if (IsEditable)
            
            {
                ucMlText.ML.UpdateForCurrentLanguage(comboBoxTitle.Text);

                // check code exists and name filled (SDMX requirements)
                if (numTextBoxValue.Text.Trim().Length == 0 || ucMlText.ML.IsEmpty)
                {
                    MessageBox.Show(this, "Required fields are not filled (code + name).", 
                        "Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    updatable = false;
                }

                // check if new code it does not exist yet
                if (IsCodeCreation)
                {
                    string codeId = numTextBoxValue.Text.Trim() + "_" + textBoxCanton.Text;
                    if (cltSchulArt.CodeList.LocateItem(codeId) != null)
                    {
                        MessageBox.Show(this, "Code is already defined: " + codeId,
                            "Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        updatable = false;
                    }
                }


                if (updatable)
                {
                    // Update code object
                    schulart.Id = numTextBoxValue.Text.Trim() + "_" + textBoxCanton.Text;

                    // Name
                    schulart.Name = schulartName;

                    // Validity
                    UpdateAnnotValidity(numTextBoxValidFrom, GridCreator.ANNOT_S_VF);
                    UpdateAnnotValidity(numTextBoxValidTo, GridCreator.ANNOT_S_VT);

                    // Combo
                    UpdateAnnotFromCheck(radCheckBoxSdl, ANNOT_SDL);
                    UpdateAnnotFromCheck(radCheckBoxSsp, ANNOT_SSP);
                    UpdateAnnotFromCheck(radCheckBoxSba, ANNOT_SBA);
                    UpdateAnnotFromCheck(radCheckBoxSbg, ANNOT_SBG);

                    //BBG nr
                    UpdateAnnotBbgNr(numTextBox1, ANNOT_BBG_NR);


                    this.DialogResult = DialogResult.OK;
                }
            }
            else // In read only, OK is same as cancel
                this.DialogResult = DialogResult.Cancel;
        }

        private void radButtonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Delete Schulart code :" + schulart.Id,
               "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                this.DialogResult = DialogResult.Yes;

        }


        #endregion

        #region Update Schulart

        private void UpdateAnnotFromCheck(RadCheckBox ckb, string annotType)
        {
            Annotation a = schulart.AnnotationList.GetAnnotationType(annotType);
            if (a == null)
            {
                a = new Annotation(annotType);
                schulart.AnnotationList.Add(a);
            }
            a.Title = ckb.Checked ? SET_MARK : MISS_MARK;
        }

        private void UpdateAnnotValidity(NumTextBox tb, string annotType)
        {
            string validity = tb.Text.Trim();
            Annotation a = schulart.AnnotationList.GetAnnotationType(annotType);

            // ensure validity annotation if validity set
            if (validity.Length > 0)
            {
                if (a == null)
                {
                    a = new Annotation(annotType);
                    schulart.AnnotationList.Add(a);
                }
                a.Title = validity;
            }
                // Remove validity annotation if validity not specified
            else if (a != null)
                schulart.AnnotationList.Remove(a);

        }

        private void UpdateAnnotBbgNr(NumTextBox tb, string annotType)
        {
            string nr = tb.Text.Trim();
            Annotation a = schulart.AnnotationList.GetAnnotationType(annotType);

            // ensure BBG annotation if number set
            if (nr.Length > 0)
            {
                if (a == null)
                {
                    a = new Annotation(annotType);
                    schulart.AnnotationList.Add(a);
                }
                a.Title = nr;
            }
            // Remove  annotation if BBG not specified
            else if (a != null)
                schulart.AnnotationList.Remove(a);
        }
        #endregion


    }
}
